ngx.req.read_body()

local requestMethod = ngx.var.request_method

if requestMethod == "POST" then
  local data = ngx.req.get_body_data()

  local cjson = require ("cjson")
--[[
  function json_decode( str )
      local json_value = nil
      pcall(function (str) json_value = cjson.decode(str) end, str)
      return json_value
  end
--]]
  -- 检查基本属性字段 "type" , "fixed_ip"
  local d = cjson.decode(data)
  if not d["type"] then
    ngx.log(ngx.ERR, "no type!")
    return
  elseif not d["fixed_ip"] then
    ngx.log(ngx.ERR, "no fixed_ip!")
    return
  end

  local mysqlPool = require ("common.dbPool")
  local db = mysqlPool:new(cmops_db_conf)
  local query_sql
  -- 根据 fixed_ip 字段检查是否存在有效的 app_id
  query_sql = "SELECT id FROM app_info WHERE fixed_ip = " .. ngx.quote_sql_str(d["fixed_ip"])
  ngx.log(ngx.INFO, 
          "sql query is : ", query_sql)
  local cusid, err, errno, sqlstate = db:query(query_sql)
  if not cusid then
    ngx.log(ngx.ERR, 
            "not app id from ip : ", d["fixed_ip"], 
            " , sqlstate : ", sqlstate)
    return
  end

  -- 根据 type 字段判断处理哪个数据表
  local sqltable
  local tmp_type = d["type"]
  if miops_tables_type(tmp_type) then
    sqltable = miops_tables_type[tmp_type]
  else
    ngx.log(ngx.ERR, 
            "DO NOT SUPPORT : ", tmp_type)
    return
  end
  ngx.say("sqltable : ", sqltable)

  -- 定义三组变量值，通过拼接满足mysql的insert和update的语句。
  local skey, svalue, sopt
  for rkey, rvalue in pairs(d) do
    if not skip_tables_field(rkey) then
      if skey then
        skey = skey .. "," .. ngx.quote_sql_str(rkey)
        svalue = svalue .. "," .. ngx.quote_sql_str(rvalue)
        sopt = sopt .. "," .. string.gsub(ngx.quote_sql_str(rkey),"'","") .. "=" .. ngx.quote_sql_str(rvalue)
      else
        skey = ngx.quote_sql_str(rkey)
        svalue = ngx.quote_sql_str(rvalue)
        sopt = string.gsub(ngx.quote_sql_str(rkey),"'","") .. "=" .. ngx.quote_sql_str(rvalue)
      end
    end
  end

  for _, row in ipairs(cusid) do
    if d["type"] ~= "app" then
      query_sql = "SELECT id FROM ".. sqltable .. 
                  " WHERE app_id = " .. ngx.quote_sql_str(row.id)
    else
      query_sql = "SELECT id FROM ".. sqltable .. 
                  " WHERE id = " .. ngx.quote_sql_str(row.id)
    end
    ngx.log(ngx.INFO, 
            "sql query is : ", query_sql)
    local queryid, err, errno, sqlstate = db:query(query_sql)
    
    if not queryid then
      ngx.log(ngx.ERR, 
              "select error : ", err, 
              " , errno : ", errno, 
              " , sqlstate : ", sqlstate)
      return
    elseif table.getn(queryid) == 0 then
      query_sql = "INSERT INTO " .. sqltable ..
                  "(" .. string.gsub(skey,"'","") .. ",app_id) " .. 
                  " VALUES (" .. svalue .. 
                  "," .. ngx.quote_sql_str(row.id) .. ")"
    else
      query_sql = "UPDATE " .. sqltable ..
                  " SET " .. sopt ..
                  " WHERE id = " .. ngx.quote_sql_str(row.id)
    end
    ngx.log(ngx.INFO, 
            "sql query is : ", query_sql)
    local res, err, errno, sqlstate = db:query(query_sql)
    if not res then
      ngx.log(ngx.ERR, 
              "select error : ", err, 
              " , errno : ", errno, 
              " , sqlstate : ", sqlstate)
      ngx.say("POST DATA ERR : ", err, "," , sqlstate)
    else
      ngx.log(ngx.INFO, 
              "POST DATA SUCCESS !")
      ngx.say("SUCCESS")
    end
  end
  ngx.flush()

end
